package engine.systems.update.tween
{
	import com.greensock.TweenMax;
	
	import ash.core.Component;
	
	import flash.utils.Dictionary;
	
	import org.as3commons.collections.LinkedMap;
	
	public class Tween extends Component
	{
		public var paused:Boolean = false;
		
		internal var tweens:LinkedMap = new LinkedMap();
		
		public function Tween() { }
		
		override public function destroy():void
		{
			while(this.tweens.size > 0)
				TweenMax(this.tweens.removeFirst()).kill();
			
			this.tweens = null;
		}
		
		public function to(object:*, time:Number, vars:Object, name:String):TweenMax
		{
			return this.add(TweenMax.to(object, time, vars), name);
		}
		
		public function from(object:*, time:Number, vars:Object, name:String):TweenMax
		{
			return this.add(TweenMax.from(object, time, vars), name);
		}
		
		public function fromTo(object:*, time:Number, fromVars:Object, toVars:Object, name:String):TweenMax
		{
			return this.add(TweenMax.fromTo(object, time, fromVars, toVars), name);
		}
		
		private function add(tween:TweenMax, name:String):TweenMax
		{
			tween.pause();
			this.tweens.add(name, tween);
			
			return tween;
		}
		
		public function get(name:String):TweenMax { return this.tweens.itemFor(name); }
		
		public function remove(name:String):void
		{
			var tween:TweenMax = this.tweens.removeKey(name);
			if(tween)
			{
				tween.kill();
				tween = null;
			}
		}
	}
}